package pers.cz.business;

import cn.hutool.core.date.DateUtil;
import pers.cz.mapper.TestCaseMapper;
import pers.cz.mapper.TestCaseParamsMapper;
import pers.cz.model.TestCase;
import pers.cz.model.TestCaseParams;
import pers.cz.mybatisPlugin.PageRowBounds;
import pers.cz.pub.AbstractBusiness;

import java.util.ArrayList;
import java.util.List;

/**
 * 查询测试案例
 * @program: PostGirl-panent
 * @description: QueryTestCase
 * @author: Cheng Zhi
 * @create: 2021-03-12 16:30
 **/
public class TestCaseBusiness extends AbstractBusiness {

    /**
     * 查询数据库中所有测试案例
     * @return
     */
    public List<TestCase> queryAllTestCase() {

        List<TestCase> testCaseList = new ArrayList<>();
        testCaseList = this.getMapper().getBean(TestCaseMapper.class).queryAllTestCase();

        return testCaseList;
    }

    /**
     * 根据接口编号查询测试案例
     * @param busiCode
     * @return
     */
    public List<TestCase> queryAllTestCase(String busiCode) {

        List<TestCase> testCaseList = new ArrayList<>();
        testCaseList = this.getMapper().getBean(TestCaseMapper.class).queryTestCaseByBusiCode(busiCode);

        return testCaseList;
    }

    /**
     * 分页查询所有案例
     * @param pageStart
     * @param pageLimit
     * @return
     */
    public List<TestCase> queryAllTestCaseByPage(Integer pageStart, Integer pageLimit, Integer userId) {

        List<TestCase> testCaseList = new ArrayList<>();
        PageRowBounds pageRowBounds = new PageRowBounds(pageStart, pageLimit);
        testCaseList = this.getMapper().getBean(TestCaseMapper.class).queryAllTestCaseByPage(pageRowBounds,userId);

        return testCaseList;

    }

    /**
     * 根据单个编号查询测试案例
     * @param id
     * @return
     */
    public TestCase queryTestCaseById(Integer id) {

        TestCase testCase = new TestCase();
        testCase = this.getMapper().getBean(TestCaseMapper.class).selectByPrimaryKey(id);

        return testCase;
    }

    /**
     * 使用id集合查询数据库中所有测试案例
     * @return
     */
    public List<TestCase> queryAllTestCaseIds(List<Integer> ids) {

        List<TestCase> testCaseList = new ArrayList<>();
        testCaseList = this.getMapper().getBean(TestCaseMapper.class).queryAllTestCaseIds(ids);

        return testCaseList;
    }

    /**
     * 根据测试案例名称获取所有测试案例
     * @param testName
     * @return
     */
    public List<TestCase> queryAllTestCaseByTestName(String testName) {

        List<TestCase> testCaseList = new ArrayList<>();
        testCaseList = this.getMapper().getBean(TestCaseMapper.class).queryAllTestCaseByTestName(testName);

        return testCaseList;
    }
    /**
     * 修改测试案例
     * @param testCase
     * @return
     */
    public Integer updateTestCase(TestCase testCase) {

        int count = this.getMapper().getBean(TestCaseMapper.class).updateByPrimaryKeySelective(testCase);
        return count;
    }

    /**
     * 添加测试案例
     * @param request
     * @param expectedResponse
     * @param busiCode
     * @param ignoreFields
     * @return
     */
    public Integer addTestCase(Integer caseId, String caseName, String request, String expectedResponse, String busiCode, String ignoreFields, String caseNameZh, Integer userId) {

        TestCase testCase = new TestCase();
        testCase.setCaseName(caseName);
        testCase.setId(caseId);
        testCase.setBusiCode(busiCode);
        testCase.setRequest(request);
        testCase.setExpectedResponse(expectedResponse);
        testCase.setIgnoreFields(ignoreFields);
        testCase.setUserId(userId);
        testCase.setResultType(2); // 新添加案例都是未执行的
        //testCase.setCreateDate(DateUtil.format(DateUtil.date(),"yyyy-MM-dd HH:mm:ss"));
        testCase.setCreateDate(DateUtil.date());
        testCase.setCaseNameZh(caseNameZh);
        int count = this.getMapper().getBean(TestCaseMapper.class).insert(testCase);
        return count;
    }

    /**
     * 删除测试案例
     * @param id
     * @return
     */
    public Integer deleteTestCase(Integer id) {

        int count = this.getMapper().getBean(TestCaseMapper.class).deleteByPrimaryKey(id);
        return count;
    }

    /**
     * 获取总数据量
     * @return
     */
    public Integer getCountTestCase(Integer userId) {
        int count = this.getMapper().getBean(TestCaseMapper.class).selectCount(userId);
        return count;
    }

    /**
     * 模糊查询测试案例
     * @param testCaseName
     * @return
     */
    public List<TestCase> queryTestCaseByFuzzy(String testCaseName) {

/*        TestCaseExample example = new TestCaseExample();
        TestCaseExample.Criteria criteria = example.createCriteria();
        criteria.andCaseNameLike("%" + testCaseName + "%");

        return this.getMapper(TestCaseMapper.class).selectByExample(example);*/
        return this.getMapper(TestCaseMapper.class).selectByFuzzy("%" + testCaseName + "%");
    }

    /**
     * 分页查询所有参数
     * @param pageStart
     * @param pageLimit
     * @return
     */
    public List<TestCaseParams> queryAllTestCaseParamsByPage(Integer pageStart, Integer pageLimit) {
        PageRowBounds pageRowBounds = new PageRowBounds(pageStart, pageLimit);
        return this.getMapper().getBean(TestCaseParamsMapper.class).queryAllTestCaseParamsByPage(pageRowBounds);
    }

    /**
     * 添加参数
     * @param testCaseParams
     * @return
     */
    public Integer addTestCaseParams(TestCaseParams testCaseParams) {

        return this.getMapper(TestCaseParamsMapper.class).insert(testCaseParams);
    }

    /**
     * 修改参数
     * @param testCaseParams
     * @return
     */
    public Integer updateTestCaseParams(TestCaseParams testCaseParams) {

        return this.getMapper(TestCaseParamsMapper.class).updateByPrimaryKeySelective(testCaseParams);
    }

    /**
     * 删除参数
     * @param id
     * @return
     */
    public Integer deleteTestCaseParams(Integer id) {

        return this.getMapper(TestCaseParamsMapper.class).deleteByPrimaryKey(id);
    }

    /**
     * 获取总数，用于分页
     * @return
     */
    public Integer getCountTestCaseParams() {

        return this.getMapper(TestCaseParamsMapper.class).getCount();

    }

    /**
     * 根据id获取参数
     * @param id
     * @return
     */
    public TestCaseParams queryTestCaseParamsById(Integer id) {
        return this.getMapper(TestCaseParamsMapper.class).selectByPrimaryKey(id);
    }
}
